|
|
|
|
|
|
|
Procedure Call Versus In-line Code |
|
|
|
|
|
|
|
|
Every time a function or procedure is called, some overhead is incurred. If you have a two-line function thats called from only three places, you might consider putting those two lines in the functions themselves and not making them a function. This can save some processing time. However, this can lead to some maintenance problems downstream. In the following example, a procedure is called to perform some calculation: |
|
|
|
 |
|
|
|
|
For lngLoop = 0 To conRepeats
Call InlineCalc
Next |
|
|
|
|
|
|
|
|
In the next example, the same code in the procedure is moved right into the loop: |
|
|
|
 |
|
|
|
|
For lngLoop = 0 To conRepeats
intOp1 = 100
intOp2 = 10
intResult = intOp1 / intOp2
Next |
|
|
|
|
|
|
|
|
Because you dont have the overhead of the procedure call, this code will execute faster. |
|
|
|
|
|
|
|
|
Cached Properties Versus Not Cached |
|
|
|
|
|
|
|
|
Retrieving values from properties is very slow. Avoid using properties in loops unless those properties are changing. Heres an example: |
|
|
|
 |
|
|
|
|
For intLoop1 = 0 To Screen.FontCount - 1
For intLoop2 = 0 To Printer.FontCount - 1
If Screen.Fonts(intLoop1) = Printer.Fonts(intLoop2) Then
strGetFont = Screen.Fonts(intLoop1)
End If
Next intLoop2
Next intLoop1 |
|
|
|
|
|
|
|
|
The inner loop retrieves the value from the Printer.FontCount property every time through the outer loop. This value does not change, so you should move it outside both the loops. In addition, you reference Screen.Fonts(intLoop1) two times within the inner loop. Again, this value is not changing, so you should cache it to a local variable prior to entering the inner loop. Heres an example: |
|
|
|
|
|